import 'package:fish_redux/fish_redux.dart';
import 'action.dart';
import 'state.dart';
import '../../../../app_files.dart';
import 'package:date_format/date_format.dart';
import '../../BindAccountView/AccountInfoView/state.dart';

Effect<SendProblemViewState> buildEffect() {
  return combineEffects(<Object, Effect<SendProblemViewState>>{
    SendProblemViewAction.onSubmit: _onSubmit,
    SendProblemViewAction.onTap: _onTap,
    Lifecycle.initState: _initState,
  });
}

void _initState(Action action, Context<SendProblemViewState> ctx) async {
  ResultData res =
  await CommonNetWork.getWorkUserInfo(accountNo: ctx.state.accountNo);
  if (res.success && res.data.length != 0) {
    GasAccountInfoModel infoModel =
    GasAccountInfoModel.fromWorkJson(res.data[0]);
    ctx.state.model = infoModel;
    ctx.state.phoneController.text = infoModel.phone;
    ctx.state.contactController.text = infoModel.name;
    ctx.state.addressController.text = infoModel.address;
    ctx.dispatch(SendProblemViewActionCreator.onAction());
  } else {
    await CommonUtils.showAlertView(ctx.context, res.desc);
    Navigator.pop(ctx.context);
  }
}

void _onSubmit(Action action, Context<SendProblemViewState> ctx) async {
  String errText = '';
  if (ctx.state.contentController.text.length == 0) {
    errText = '请输入反馈内容';
  }
  if (ctx.state.time == null) {
    errText = '请选择预约时间';
  }
  if (ctx.state.addressController.text.length == 0) {
    errText = '请输入报修地址';
  }
  if (ctx.state.phoneController.text.length == 0) {
    errText = '请输入联系方式';
  }
  if (ctx.state.contactController.text.length == 0) {
    errText = '请输入联系人';
  }
  if (errText.length != 0) {
    CommonUtils.showErrorInfo(ctx.context, errText);
    return;
  }
  CommonUtils.closeKey(ctx.context);

  List<String> uploads = [];
  if (ctx.state.images.length != 0) {
    CommonUtils.showLoadingDialog(ctx.context);

    uploads = await CommonNetWork.multiUploadImgs(
      ctx.state.images,
      userNo: ctx.state.model.accountNo,
    );
    Navigator.pop(ctx.context);
  }

  CommonUtils.showLoadingDialog(ctx.context);
  ResultData res = await CommonNetWork.addWorkOrder(
    photo: uploads.join(','),
    addTime: ctx.state.time,
    gasFactory: ctx.state.model.gasFactory,
    communityName: ctx.state.model.communityName,
    communityID: ctx.state.model.communityID,
    content: ctx.state.contentController.text,
    accountNo: ctx.state.model.accountNo,
    gasNo: ctx.state.model.gasNo,
    accountMobile: ctx.state.phoneController.text,
    accountAddress: ctx.state.addressController.text,
    accountName: ctx.state.contactController.text,
    accountID: ctx.state.model.accountID,
  );
  Navigator.pop(ctx.context);
  if (res.success) {
    await CommonUtils.showAlertView(ctx.context, '预约成功');
    Navigator.pop(ctx.context);
    Navigator.pop(ctx.context);
  } else {
    CommonUtils.showErrorInfo(ctx.context, res.desc);
  }
}

void _onTap(Action action, Context<SendProblemViewState> ctx) async {
  CommonUtils.closeKey(ctx.context);
  if (action.payload == 1) {
    DateTime res = DateTime.now();
    if (ctx.state.time != null)
      res = DateTime.tryParse(ctx.state.time) ?? DateTime.now();
    res = await CommonUtils.showCupertinoDatePicker(ctx.context, res,
        mode: CupertinoDatePickerMode.dateAndTime);
    print(res);
    if (res != null) {
      String beginTime =
      formatDate(res, [yyyy, '-', mm, '-', dd, ' ', HH, ':', nn,':', ss]);
      ctx.state.time = beginTime;
      ctx.dispatch(SendProblemViewActionCreator.onAction());
    }
  }
}
